﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;
using CubicFWNet.Data.SystemManager;

namespace CubicFWNet.Application.SystemManager
{
    public class StorageTableApp
    {
        private readonly StorageTableRepository _service = new StorageTableRepository();
        private readonly InvoiceRepository _invoiceRepository = new InvoiceRepository();
        private readonly DeliveryProductRepository _deliveryProductRepository = new DeliveryProductRepository();

        public int Add(StorageTable web)
        {
            return _service.Insert(web);
        }

        public int Update(StorageTable web)
        {
            return _service.Update(web);
        }

        public StorageTable GetSinglExamine(Guid id)
        {
            return _service.FindEntity(a => a.Id == id);
        }


        /// <summary>
        /// 查询出库入库单列表
        /// </summary>
        /// <param name="type">1：出库  2：入库</param>
        /// <param name="proType">1：产品 2：原料</param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public IEnumerable<StorageTableView> GetList(int type, DateTime start, DateTime end, string name)
        {
            return _service.GetList(type, start, end, name);
        }

        public IEnumerable<StorageTableView> GetList(int type, Guid pid, int sid)
        {
            return _service.GetList(type, pid, sid);
        }

        /// <summary>
        /// 判断发货单下产品是否出库完成
        /// </summary>
        /// <param name="invID"></param>
        /// <returns></returns>
        public bool GetOutCount(Guid invID)
        {
            int count1 = _service.IQueryable().Count();
            int count2 = _deliveryProductRepository.IQueryable(a => a.InvoiceId == invID).Count();
            if (count1 == count2)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取出库入库详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StorageTableView GetFullInfo(Guid id)
        {
            return _service.GetSTInfo(id);
        }

        public StorageTable GetInfo(Guid id)
        {
            return _service.FindEntity(a => a.Id == id);
        }

        /// <summary>
        /// 出库完成后，更新发货单状态
        /// </summary>
        /// <param name="invoiceID"></param>
        /// <returns></returns>
        public void UpdateInvoiceStatus(Guid invoiceID)
        {
            _invoiceRepository.Update(a => a.Id == invoiceID, a => new Invoice { Status = 1 });
        }

        /// <summary>
        /// 获取未审核出库单数量
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public int GetNoCheckCount(Guid inviceID)
        {
            //return _service.IQueryable(a => a.Type == 1 && a.InvoiceID == inviceID && a.Status == -1).Count();
            return _service.IQueryable(a => a.Type == 1).Count();
        }

        /// <summary>
        /// 根据关系表firstID获取订单列表 [去重]
        /// </summary>
        /// <param name="type"></param>
        /// <param name="fID"></param>
        /// <param name="proID"></param>
        /// <returns></returns>
        public IEnumerable<OrderRelationView> GetOrderByRealtion(string type, Guid fID, Guid proID)
        {
            List<OrderRelationView> ls = _service.GetOrderByRealtion(type, fID, proID).ToList();
            List<OrderRelationView> lsNew = new List<OrderRelationView>();
            List<Guid> lsID = new List<Guid>();

            for (int i = 0; i < ls.Count(); i++)
            {
                lsID.Add(ls[i].OrderId.Value);
            }
            //productID 去重
            HashSet<Guid> lsNewID = new HashSet<Guid>(lsID);

            foreach (var obj in lsNewID)
            {
                for (int j = 0; j < ls.Count(); j++)
                {
                    if (obj == ls[j].OrderId)
                    {
                        OrderRelationView o = new OrderRelationView();
                        o.CustomeId = ls[j].CustomeId;
                        o.CustomerName = ls[j].CustomerName;
                        o.Memo = ls[j].Memo;
                        o.Number = ls[j].Number;
                        o.OrderId = ls[j].OrderId;
                        o.ProductName = ls[j].ProductName;
                        o.UserId = ls[j].UserId;
                        o.UserName = ls[j].UserName;
                        o.CusProName = ls[j].CusProName;
                        lsNew.Add(o);
                        break;
                    }
                }
            }

            return lsNew;
        }

    }
}
